package com.g4m.baselib.interceptor.config;

import com.g4m.baselib.interceptor.admin.GlobalAdminRequestInterceptor;
import com.g4m.baselib.interceptor.global.GlobalRequestInterceptor;
import com.g4m.baselib.interceptor.web.GlobalWebRequestInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * haosenwei[haosenwei@hudesen.com]
 * 21/02/2021 11:18
 * <p>Copyright 2008-2019 snsndk.com</p>
 */
@Configuration
public class InterceptorConfig extends WebMvcConfigurationSupport {

    @Autowired
    private GlobalRequestInterceptor globalRequestInterceptor;

    @Autowired
    private GlobalAdminRequestInterceptor globalAdminRequestInterceptor;

    @Autowired
    private GlobalWebRequestInterceptor globalWebRequestInterceptor;

    /**
     * 配置
     *
     * @param registry 注册
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(this.globalRequestInterceptor).addPathPatterns("/**").excludePathPatterns("/favicon.ico", "/robots.txt", "index.htm", "index.html");
        registry.addInterceptor(this.globalAdminRequestInterceptor).addPathPatterns("/admin/**", "/*/admin/**").excludePathPatterns("/favicon.ico", "/robots.txt", "index.htm", "index.html");
        registry.addInterceptor(this.globalWebRequestInterceptor).addPathPatterns("/**").excludePathPatterns("/favicon.ico", "/robots.txt", "index.htm", "index.html");
        super.addInterceptors(registry);
    }

    /**
     * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新指定静态资源
     *
     * @param registry 注册器
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }
}
